package com.sali.controller;

import com.sali.service.AlipayService;
import com.sali.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
public class AlipayController {

    @Autowired
    private AlipayService alipayService;


    @GetMapping("/alipay/pay")
    @ResponseBody
    public byte[] alipay() {
        PayVo payVo = new PayVo();
        payVo.setUserId("user-1");
        payVo.setStoreId("store-1");
        payVo.setProductId("product-1");
        payVo.setOrderNumber("ordernumber-1");
        payVo.setProductName("product-name-1");
        payVo.setPtype("product-type-1");
        payVo.setMoney("11.1");

        byte[] qrByteForAlipay = alipayService.alipay(payVo);

        return qrByteForAlipay;
    }

    /**
     * 定义支付回调的地址
     * <p>
     * 1：第一步：打成一个jar 发布正式服务器
     * 2：第二步：购买一个域名：https:/www.kuangstudy.com/
     * 3：第三步：部署的项目到服务器上。java -jar ksd-alipay.jar >>1.txt &
     * 4：第四步：获取真实的回调地址  https:/www.kuangstudy.com/alipay/notifyUrl?body=&param&
     */
    @ResponseBody
    @RequestMapping("/alipay/notifyUrl")
    public String notify_url(HttpServletRequest request) throws Exception {

        boolean result = alipayService.alipayCallback(request);

        if (result) {
            return "success";
        } else {
            // 验证失败
            return "fail";
        }
    }
}
